<template>
	<view class="mainpadding">
		<view class="flexleft margin_top">
			<view class="margin_right1">
				<u-icon name="map"></u-icon>
			</view>
			<view class="ershiba xiaohui">当前城市</view>
		</view>
		<view class="flexbetween margin_top">
			<view class="sanshier">{{citytext}}</view>
			<view class="flexright"  @click="getlocation">
				<image class="weizhi margin_right1" src="../static/image/system/dw.png" mode=""></image>
				<view class="ershiba xiaolan">重新定位</view>
			</view>
		</view>
		<view class="flexleft margin_top">
			<view class="margin_right1">
				<image class="weizhi margin_right1" src="../static/image/system/xz.png" mode=""></image>
			</view>
			<view class="ershiba xiaohui">选择城市</view>
		</view>
		<view class="margin_top">
			<u-index-list :index-list="indexList">
				<template v-for="(item, index) in itemArr">
					<u-index-item>
						<u-index-anchor :text="item.key"></u-index-anchor>
						<view class="list-cell" v-for="(cell, inde) in item.list" :key="inde" @click="checkcity(cell)">
							{{cell.name}}
						</view>
					</u-index-item>
				</template>
			</u-index-list>
		</view>
	</view>
</template>

<script>0
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				longitude: "",
				latitude: "",
				citytext: "",
				cityid:"",
				indexList: [],
				itemArr: [],
				type:0
			}
		},
		onLoad(options) {
			this.type = options.type
			let storagecity = uni.getStorageSync("storagecity") || null
			if(storagecity){
				this.citytext = storagecity.citytext
				this.latitude = storagecity.latitude
				this.longitude = storagecity.longitude
				this.cityid = storagecity.cityid
			}else{
				this.getlocation()
			}
			this.getcitylist()
		},
		methods: {
			getlocation() {
				uni.showLoading({
					mask:true,
					title:"获取定位中"
				})
				let _this = this
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						_this.latitude = res.latitude
						_this.longitude = res.longitude
						_this.getcityInfo()
						_this.setuserlat()
					},
					fail(err) {
						uni.hideLoading()
						_this.openAuthSetting();
						console.log(err, 23432);
					}
				});
			},
			setuserlat(){
				if (!httpRequest.checkIsLogin()) {
					return false
				}
				httpRequest.request('/api/user/profile', 'GET', {latitude:this.latitude,longitude:this.longitude,}).then(res => {})
			},
			getcityInfo() {
				httpRequest.request('/api/index/getCurrCity', 'POST', {
					lat: this.latitude,
					lng: this.longitude
				}, false, false, true).then(res => {
					uni.hideLoading()
					let data = {
						citytext:res.data.name,
						latitude:this.latitude,
						longitude:this.longitude,
						cityid:res.data.id,
					}
					this.citytext = res.data.name
					this.cityid = res.data.id
					uni.setStorageSync("storagecity", data)
				})
			},
			checkcity(item){
				let data = {
					citytext:item.name,
					latitude:item.lat,
					longitude:item.lng,
					cityid:item.id
				}
				this.cityid = item.id
				this.longitude = item.lng
				this.latitude = item.lat
				this.citytext = item.name
				uni.$emit("selectaddress",data)
				if(this.type!=1){
					uni.setStorageSync("storagecity",data)
				}
			},
			getcitylist(){
				httpRequest.request('/api/index/getCityList', 'POST', {
					keywords:this.keywords
				}, false, false, true).then(res => {
					let indexList = []
					res.data.forEach(item=>{
						indexList.push(item.key)
					})
					this.itemArr = res.data
					this.indexList = indexList
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	/deep/.u-border-bottom{
		background-color: #fff !important;
		border: none;
	}
	.list-cell {
		display: flex;
		box-sizing: border-box;
		width: 100%;
		padding: 10px 24rpx;
		overflow: hidden;
		color: #323233;
		font-size: 14px;
		line-height: 24px;
		border-bottom: 1rpx solid rgba(0, 0, 0, .06);
		background-color: #fff;
	}

	.weizhi {
		width: 28rpx;
		height: 28rpx;
	}
</style>